<?php
/**
 * 技术部离职员工
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Tools_SendMail');
apf_require_class('Bll_Hr_OutInterface');
class Oa_Job_Flow_TechDimissionUsers extends Oa_Job_Abstract {
    private $write_db = "hroa_master";//读数据库

    public function run($params) {
        $time_start = time();
        $this->log("开始执行啦！".date('Y-m-d H:i:s'));
        $this->hroa_bll = new Bll_Hr_OutInterface();

        $this->to_email = array(
                'dl-techmgr@anjuke.com'
        );
        $this->allow_function=array(
                6,
                //11,//BI
                13,14,15,16
                );
        $w = date('w');
        if($w==1){
            $this->_week_dimission();
        }

        $time_end = time();
        $this->log('(End Job--'.date('Y-m-d H:i:s').')'.($time_end-$time_start).'seconds');
    }

    private function _week_dimission(){
        $start =time()-7*86400;
        $end = time()-86400;
        $sql = "select * from user where job_status=2 and last_date>= ".date('Ymd',$start)." and last_date<=".date('Ymd',$end);
        $tmp = $this->get_db_rows($sql,$this->write_db,array());
        if(!empty($tmp)){
            $sv=date('Y-m-d',$start);
            $sd=date('Y-m-d',$end);
            $mailcontent = $this->_build_mail_template($tmp, $sv, $sd);
            $this->_email_send($this->to_email,'#'.$sd.'#技术离职员工',$mailcontent);
        }
    }
    private function _email_send($to, $title, $body, $from = "hr@anjuke.com"){
        $mail = new PHPMailer(true);
        $mail->CharSet = "UTF-8";
        $mail->IsSMTP();
        $mail->SMTPDebug = 0;

        $mail->SMTPAuth = true;
        $mail->Host = "smtp.anjuke.com";
        $mail->Port = 25;
        $mail->Username = "hr@anjuke.com";
        $mail->Password = "anjuke1qazxsw2";
        if(is_array($to)){
            foreach ($to as $val){
                $mail->AddAddress($val, $val);
            }
        }else{
            $mail->AddAddress($to, $to);
        }

        $mail->SetFrom($from, '人力资源部');
        $mail->Subject = $title;
        $mail->AltBody = ' ';
        $mail->MsgHTML($body);
        $mail->Send();
    }

    private function _get_user_work_info($uid){
        $sql = "SELECT * FROM work WHERE user_id={$uid} order by work_id desc limit 1 ";
        $tmp = $this->get_db_rows($sql,$this->write_db,array());
        $workdata = $tmp[0];
        $job_data = $this->hroa_bll->get_job_dep_info($workdata['job_id']);

        $indata['job_id']=$workdata['job_id'];
        $indata['position_name']=$job_data['position_name'];
        $indata['plan_num']=intval($job_data['job_plan_num']);
        $indata['fact_num']=intval($job_data['job_fact_num']);
        $indata['department_id']=intval($job_data['department_id']);
        $indata['department_name']=$job_data['department_name'];
        $indata['function_id']=intval($job_data['function_id']);
        $indata['bu_id']=intval($job_data['bu_id']);
        return $indata;


    }
    private function _build_mail_template($data,$start,$end) {
        $tdstyle = "border: 1px solid #C3D7DB;padding: 2px 3px;";
        $result = "Dear All:<br><br>".$start."到".$end."离职人员名单：<br><br>
        <table style='width:900px;font-size:11px;font-family: Sans,Verdana,Tahoma,Arial,Simsun;width:800px;border: 1px solid #C3D7DB;border-radius: 3px 3px 3px 3px;box-shadow: 1px 1px 5px #5D99A3;color: #294145;font-size: 11px;font-weight: bold;line-height: 20px;margin: 10px;min-width: 100px;padding: 3px 10px;'>
        <tbody><tr><td>";
        $result .="<table style='border-collapse:collapse;cellpadding:0;cellspacing:0;border:1px solid #C3D7DB;width:100%;'>
        <thread style='background: none repeat scroll 0 0 #DDEEEE;'>
        <tr style='line-height: 18px;background-color:#DDEEEE;font-weight: bold;'>
        <td style='{$tdstyle}' align='center'></td>
        <td style='{$tdstyle}' align='center'>姓名</td>
        <td width='150px' style='{$tdstyle}' align='center'>邮箱</td>
        <td style='{$tdstyle}' align='center'>部门</td>
        <td style='{$tdstyle}' align='center'>职位</td>
        </tr></thread>";
        $count = count($data['datalist']);
        $num = 1;
        $uids = array();
        foreach ($data as $key => $val) {
            if(!in_array($val['user_type'],array(1,2,3))){
                continue;
            }
            if($val['leaving_type']==3){
                continue;
            }
            $info = $this->_get_user_work_info($val['user_id']);
            if(!in_array($info['function_id'],$this->allow_function)){
                continue;
            }
            if(in_array($val['user_id'],$uids)){
                continue;
            }
            $uids[]=$val['user_id'];
            $result .= "<tr><td align='center' style='{$tdstyle}'>".$num."</td><td align='center' style='{$tdstyle}'>".$val['user_name']."</td><td align='center' style='{$tdstyle}'>".$val['office_mail']."</td><td align='center' style='{$tdstyle}'>".$info['department_name']."</td><td align='center' style='{$tdstyle}'>".$info['position_name']."</td></tr>";
            $num++;
        }
        $result .= "</table></td></tr></tbody></table>";
        return $result;
    }
}